﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TouchUI.System.Mobile.Mvc;
using System.Mobile.Mvc;

using LinkedService.Mobile.ServiceEntities;
using System.Drawing;
using System.Windows.Forms;
using LinkedService.Mobile;
using LinkedIn.Mobile.Properties;

namespace LinkedIn.Mobile
{

  /// <summary>
  /// Dialog to show the different recent updates
  /// </summary>
  public class UpdatesDialog : ViewDialog, IView<Updates>
  {

    /// <summary>
    /// Event to go back in history
    /// </summary>
    [PublishEvent("OnBack")]
    public event EventHandler OnBackEvent;

    /// <summary>
    /// A profile has been selected, move to this profile
    /// </summary>
    [PublishEvent("OnProfileSelected")]
    public event PersonEventHandler OnProfileSelected;

    private ScrollList scrollList;

    /// <summary>
    /// Constructor
    /// </summary>
    public UpdatesDialog()
    {
      // Initialize the screen size nad scrolllist
      ScreenFactor = Common.Instance.ScreenFactor;
      Rectangle = Common.Instance.ClientRectangle;
      scrollList = new ScrollList()
      {
        Rectangle = new Rectangle(0, 0, Rectangle.Width, Rectangle.Height),
        ListItemHeight = UpdateListItem.ItemHeight,
        ScreenFactor = ScreenFactor,
        HighBrush = Common.Instance.HighBrush,
        GrayBrush = Common.Instance.GrayBrush
      };

      HasScrollControl = true;
      scrollList.ScrollHeight = 0;
      // What to do when we click on an update.
      scrollList.Selected += new EventHandler(scrollList_Selected);
    }

    /// <summary>
    /// Handle the selectionchanged event from the scrolllist
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void scrollList_Selected(object sender, EventArgs e)
    {
      UpdateListItem item = scrollList.ListItems[scrollList.SelectedLine] as UpdateListItem;
      if (item != null)
      {
        // Notify our controllor
        if (OnProfileSelected != null)
            OnProfileSelected(this, new PersonEventArgs() { Person = item.ContainedItem as Person, People = item.People, Id = item.Id, Distance = item.Distance });
      }
    }

    private string Today = DateTime.Now.Date.ToString(Strings.DateFormat);
    private string lastheader = " ";

    /// <summary>
    /// Add a headeritem in case we changed dates
    /// </summary>
    /// <param name="u"></param>
    private void AddHeader(Update u)
    {
      string date = UnixTime.FromUnixTime(u.Timestamp / 1000).Date.ToString(Strings.DateFormat);
      if (date.CompareTo(lastheader) != 0)
        scrollList.ListItems.Add(new HeaderListItem(date.CompareTo(Today) == 0 ? Strings.Today : date));
      lastheader = date;

    }
    /// <summary>
    /// Callback from the Controller, when all updates have been loaded
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnUpdatesLoaded(object sender, EventArgs e)
    {
      // If there where any result, add them to the list.
      if (Model.Total > 0)
      {
        for (int i = 0; i < Model.Items.Count; i++)
        {
          AddHeader(Model.Items[i]);
          scrollList.ListItems.Add(UpdateItemFactory.Create(i, Model.Items[i]));
        }
        //Make sure we can scroll all the way down.
        scrollList.ScrollHeight = scrollList.ListItemHeight * scrollList.ListItems.Count;
      }
    }

    /// <summary>
    /// Do our custom painting
    /// </summary>
    /// <param name="g"></param>
    public override void Paint(Graphics g)
    {
      g.Clear(BackColor);
      // Let the scrolllist do its painting
      scrollList.Paint(g);
    }


    int headerHeight = 0;
    /// <summary>
    /// What to do when we receive a MouseDown event.
    /// </summary>
    /// <param name="p"></param>
    public override void MouseDown(Point p)
    {
      // Propogate
      scrollList.MouseDown(new Point(p.X, p.Y - headerHeight));
    }

    /// <summary>
    /// What to do when we receive a MouseMove event.
    /// </summary>
    /// <param name="p"></param>
    public override void MouseMove(Point p)
    {
      // Propogate
      scrollList.MouseMove(new Point(p.X, p.Y - headerHeight));
    }

    /// <summary>
    /// What to do when we receive a MouseUp event.
    /// </summary>
    /// <param name="p"></param>
    public override void MouseUp(Point p)
    {
      // Propogate
      scrollList.MouseUp(new Point(p.X, p.Y - headerHeight));
    }

    /// <summary>
    /// What to do when we receive a Keydown event.
    /// </summary>
    /// <param name="e"></param>
    public override void KeyDown(KeyEventArgs e)
    {
      // Propogate
      scrollList.KeyDown(e);
    }

    /// <summary>
    /// What to do when we receive a KeyUp event.
    /// </summary>
    /// <param name="e"></param>
    public override void KeyUp(KeyEventArgs e)
    {
      // Propogate
      scrollList.KeyUp(e);
    }

    /// <summary>
    /// Resize the control
    /// </summary>
    /// <param name="r"></param>
    public override void Resize(Rectangle r)
    {
      // First ourselves.
      this.Rectangle = r;
      scrollList.Resize(r);
      // Now the scrolllist
      //scrollList.Rectangle = new Rectangle(0, headerHeight, r.Width, r.Height - headerHeight - 50);
    }


    #region IView<L> Members

    /// <summary>
    /// The view data to interact with the controller.
    /// </summary>
    public new ViewDataDictionary<Updates> ViewData
    {
      get;
      set;
    }

    public new Updates Model
    {
      get
      {
        return this.ViewData.Model;
      }
      set
      {
        this.ViewData.Model = value;
      }
    }

    #endregion
  }
}
